<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
    class User{
        
        function connectData()
        {
            $con = mysql_connect("localhost","root", "" );
            if (!$con)
            {
                die('Dont connect to Database: ' . mysql_error());
            }
            mysql_select_db("registerdb", $con);
        }
     /*  XML structure
            <DataSet>
              <UserInfo>
                <Id>1</Id>
                <Name>Nhat</Name>
                <Email>nguyen89nhat@gmail.com</Email>
                <Flag>0</Flag>
              </UserInfo>
              <UserInfo>
                <Id>2</Id>
                <Name>Trang</Name>
                <Email>Trang66@gmail.com</Email>
                <Flag>0</Flag>
              </UserInfo>
            </DataSet>
      */
        
    //============================================================================================//
        function insertData($xmlstring)
        {
            $this->connectData();
            $xml = new SimpleXMLElement($xmlstring);
            //$table_name = $xml->getName();
            $table = $xml->children()->getName();
        // Convert string xml to array data    
            $parser = xml_parser_create();
            xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
            xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
            xml_parse_into_struct($parser, $xmlstring, $values, $tags);
            xml_parser_free($parser);
            
            foreach ($tags as $key => $val) 
            {
                if($key == $table)
                {
                    $table_rang = $val;
                    for ($i=0; $i < count($table_rang); $i+=2) 
                    {
                        $offset = $table_rang[$i] + 1;
                        $len = $table_rang[$i + 1] - $offset;
                        $row_arr = array_slice($values, $offset, $len);
                        for($j = 0; $j < count($row_arr); $j++)
                        {
                            $row[$row_arr[$j]["tag"]] = $row_arr[$j]["value"];
                        }
                        
                        $fields = implode(',',array_keys($row));
                        $values_f = implode("','", $row);
                        
                        $query = "insert into $table($fields) values('".$values_f."')";
                        mysql_query($query);
                    }
                }
                else {continue;}
                
            }
        }
        
   //============================================================================================//
        /*  XML structure
            <DataSet>
              <UserInfo>
                <Id>1</Id>
                <Name>Nhat</Name>
                <Email>nguyen89nhat@gmail.com</Email>
                <Flag>0</Flag>
              </UserInfo>
              <UserInfo>
                <Id>2</Id>
                <Name>Trang</Name>
                <Email>Trang66@gmail.com</Email>
                <Flag>0</Flag>
              </UserInfo>
            </DataSet>
      */
        
        function selectData()
        {
           // $xml = new SimpleXMLElement("<DataSet></DataSet>");
            $xml = new XMLWriter();
            $xml->openMemory();
            $xml->startDocument();
            $xml->startElement("DataSet");
            
            $this->connectData();
            $query = "select * from UserInfo where Flag = 0";
            $result = mysql_query($query);
       // convert data to xml
            if(mysql_num_rows($result) > 0)
            {
                while($result_arr = mysql_fetch_assoc($result))
                {
                    $xml->startElement("UserInfo");
               //     $xml->addChild("Table");
                    foreach ($result_arr as $key => $value) {
                       // $xml->addChild($key,$value);
                        $xml->startElement($key);
                        $xml->writeRaw($value);
                        $xml->endElement();
                    }
                    $xml->endElement();
                }
            }
            $xml->endElement();
         //   echo $xml->asXML();
            //return $xml->asXML();
            return $xml->outputMemory();
        }
    //============================================================================================//
    }
/*[Test]    $xmlstr = "<DataSet>
              <UserInfo>
                <Id>1</Id>
                <Name>Nhat</Name>
                <Email>nguyen89nhat@gmail.com</Email>
                <Flag>0</Flag>
              </UserInfo>
              <UserInfo>
                <Id>2</Id>
                <Name>Trang</Name>
                <Email>Trang66@gmail.com</Email>
                <Flag>0</Flag>
              </UserInfo>
            </DataSet>";
    function abc()
    {
        $us = new User;
        $us->selectData();
    }
    abc();*/
    
?>
